package fei.yuzhi.com.loveflowers.fragment.mefragment.login;

import android.util.Log;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import fei.yuzhi.com.loveflowers.fragment.mefragment.login.register.UserBean;

/**
 * Created by Administrator on 2016/10/7.
 */
public class LoginModel implements LoginContract.ModelLogin{

     public interface userLogin{
         void getLoginRegisterAccesss(String msg,UserBean user);
         void getLoginRegisterFail(String msg);
     }

    @Override
    public void userLogin(String name, final String pass, final userLogin login) {


            //查询和输入框name相同的条目
            BmobQuery<UserBean> query = new BmobQuery<>();
            query.addWhereEqualTo("name",name);
            // query.addWhereEqualTo("pass",et_pass.getText().toString());

            query.setLimit(50);

            query.findObjects(new FindListener<UserBean>() {
                @Override
                public void done(List<UserBean> list, BmobException e) {

                    if (e == null) {
                        //若密码也相同，则登陆成功
                        if (list.get(0).getPass().equals(pass)) {
                           // Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                            Log.i("tag", "登陆成功");
                            login.getLoginRegisterAccesss( "登陆成功",list.get(0));
                        } else {
                          //  Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                            Log.i("tag", "登录失败");
                            login.getLoginRegisterFail( "登录失败");
                        }
                    } else {
                      //  Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                        Log.i("tag", "登录失败");
                        login.getLoginRegisterFail( "登录失败");
                    }
                }
            });


    }

}
